
class BinaryTree {
    static class TreeNode {
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }

    public TreeNode lowestCommonAncestor(TreeNode root,TreeNode p,TreeNode q) {
        if (root == null) {
            return null;
        }
        if (root == p || root == q) {
            return root;
        }
        TreeNode leftResult = lowestCommonAncestor(root.left,p,q);
        TreeNode rightResult = lowestCommonAncestor(root.right,p,q);
        if (leftResult != null && rightResult != null) {
            return root;
        } else if (leftResult != null) {
            return leftResult;
        } else {
            return rightResult;
        }
    }



}
public class Test {
    public static void main(String[] args) {

    }
}